package com.NeuglsWorkStudio.UI
{
	import com.NeuglsWorkStudio.Colors.HSL;
	import com.NeuglsWorkStudio.UI.FormElement.Component.TComponent;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TFormExConfig;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	
	/**This class have done much more works for you to display the TForm class.
	 * This class is Recommanded
	 * */
	public class TFormEx extends Sprite
	{
		private var _FConfig:TFormExConfig;
		private var _FForm:TForm;
		private var _FBack:Sprite;

		public function get OnShow():Function
		{
			return _FOnShow;
		}

		public function set OnShow(value:Function):void
		{
			_FOnShow = value;
		}

		private var _FOnShow:Function;

		public function get OnClose():Function
		{
			return _FOnClose;
		}

		public function set OnClose(value:Function):void
		{
			_FOnClose = value;
		}

		private var _FOnClose:Function=null;
		public static var FormAnimateTime:Number=0.6;
		
		
		/**
		 * @param FormConfig The Configure information of the form*/
		public function TFormEx(FormConfig:TFormExConfig)
		{
			super();
			_FConfig=FormConfig;
			
			Init();
		}
		
		protected function Init():void{
			_FForm=new TForm(_FConfig.BaseColor);
			_FForm.CloseBtnTip=_FConfig.CloseBtnTip;
			_FForm.Height=_FConfig.Height;
			_FForm.Width=_FConfig.Width;
			_FForm.Resize();
			_FForm.Caption=_FConfig.Caption;
			_FForm.Icon=_FConfig.Icon;
			
			_FForm.AddComponents(_FConfig.Components);
			_FForm.OnClose=FOnClose;
			
			this.addEventListener(Event.ADDED_TO_STAGE,function(e:Event):void{
				_FBack=new Sprite();
				buildBackground(_FBack,stage.stageWidth,stage.stageHeight);
				stage.addChild(_FBack);
				_FBack.alpha=0;
				_FForm.alpha=0;
				stage.addChild(_FForm);
				TweenLite.to(_FBack,FormAnimateTime,{alpha:0.8});
				TweenLite.to(_FForm,FormAnimateTime,{alpha:1,onComplete:function():void{
					if(_FOnShow!=null){
						_FOnShow();
					}
				}});
				Paint();
				stage.addEventListener(Event.RESIZE,resize);
			},false,0,true);
		}
		
		public function Resize():void{
			resize(null);
		}
		
		private function resize(e:Event):void{
			buildBackground(_FBack,stage.stageWidth,stage.stageHeight);
			Paint();
		}
		
		protected function Paint():void{
			with(_FForm){
				x=(stage.stageWidth-width)/2;
				y=(stage.stageHeight-height)/2;
			}
		}
		
		private function FOnClose():void{
			TweenLite.to(_FBack,FormAnimateTime,{alpha:0});
			TweenLite.to(_FForm,FormAnimateTime,{alpha:0,onComplete:function(e:DisplayObjectContainer):void{
				stage.removeChild(_FBack);			
				stage.removeChild(_FForm);
				_FBack=null;
				_FForm=null;
				stage.removeEventListener(Event.RESIZE,resize);
				if(_FOnClose!=null){
					_FOnClose();
				}
			},onCompleteParams:[this.parent]});
		}
		
		private function buildBackground(O:Sprite,w:Number,h:Number):void{
			var result:Sprite=O;
			with(result.graphics){
				clear();
				//beginFill(HSL.AddLightToColor(_FConfig.BaseColor,-0.2),0.8);
				beginFill(0x222222,0.8);
				drawRect(0,0,w,h);
				endFill();
			}
			result.filters=[new BlurFilter(6,6,2)];
		}
		
		public function CloseForm():void{
			FOnClose();
		}
		
		public function FindComponent(AName:String):TComponent{
			return _FForm.GetComponentByName(AName);
		}
	}
}